/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package day2ixj;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class MyAPI {

    private Connection conn;
    private PreparedStatement pstm;
    private ResultSet rs;

    private Connection makeConn() {
        try {
            Class.forName("");
            conn = DriverManager.getConnection("", "", "");
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Order> getAllOrder() {
        try {
            makeConn();
            List<Order> list = new ArrayList<Order>();
            pstm = conn.prepareStatement("select * from Order");
            rs = pstm.executeQuery();
            while (rs.next()) {
                Order o = new Order(rs.getInt(1), rs.getInt(2),
                        rs.getDouble(3));
                list.add(o);
            }
            return list;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    //

    public List<OrderDetail> getAllOrderDetail(int orderid) {
        try {
            makeConn();
            List<OrderDetail> list = new ArrayList<OrderDetail>();
            pstm = conn.prepareStatement("select * from OrderDetail where"
                    + " orderid = ?");
            pstm.setInt(1, orderid);
            rs = pstm.executeQuery();
            while (rs.next()) {
                OrderDetail od = new OrderDetail(
                        rs.getInt(2),rs.getInt(3));
                list.add(od);
            }
            return list;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }
    }

   public String getCustomerName(int custID){
        try {
            makeConn();
            pstm = conn.prepareStatement("select customername "
                    + "from Customer where"
                    + " custid = ?");
            pstm.setInt(1, custID);
            rs = pstm.executeQuery();
            if(rs.next()){
                return rs.getString(1);
            }else{
                return "";
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return "";
        }
    }

    public String getProductName(int custID){
        try {
            makeConn();
            pstm = conn.prepareStatement("select productname "
                    + "from Product where"
                    + " productid = ?");
            pstm.setInt(1, custID);
            rs = pstm.executeQuery();
            if(rs.next()){
                return rs.getString(1);
            }else{
                return "";
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return "";
        }
    }
}
